package com.yjhb.android.sbz.activity;

import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.borjabravo.readmoretextview.ReadMoreTextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.dialog.TipOffDialog;
import com.yjhb.android.sbz.entity.Report;
import com.yjhb.android.sbz.entity.Task;
import com.yjhb.android.sbz.mvp.report.TipOffPresenter;
import com.yjhb.android.sbz.mvp.report.TipOffView;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.tool.ZxLogTool;
import com.jojo.android.zxlib.tool.ZxTimeTool;
import com.jojo.android.zxlib.view.nine.ItemImageClickListener;
import com.jojo.android.zxlib.view.nine.NineGridImageView;
import com.jojo.android.zxlib.view.nine.NineGridImageViewAdapter;
import com.jojo.android.zxlib.view.preview.PreviewBuilder;
import com.jojo.android.zxlib.view.preview.enitity.IPreviewInfo;
import com.jojo.android.zxlib.view.preview.util.PreviewUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/25 9:50
 */
public class ReportContentActivity extends BaseActivity<TipOffPresenter> implements ItemImageClickListener, TipOffView {
    private final String TAG = "ReportContentActivty";

    @BindView(R.id.info1_tv)
    TextView info1Tv;
    @BindView(R.id.content1_tv)
    ReadMoreTextView content1Tv;
    @BindView(R.id.nineGridImageView1)
    NineGridImageView nineGridImageView1;
    @BindView(R.id.info2_tv)
    TextView info2Tv;
    @BindView(R.id.content2_tv)
    ReadMoreTextView content2Tv;
    @BindView(R.id.nineGridImageView2)
    NineGridImageView nineGridImageView2;
    @BindView(R.id.head1_iv)
    ImageView head1Iv;
    @BindView(R.id.head2_iv)
    ImageView head2Iv;
    @BindView(R.id.layout1)
    LinearLayout layout1;
    @BindView(R.id.layout2)
    LinearLayout layout2;
    @BindView(R.id.task_title_tv)
    TextView task_title_tv;
    @BindView(R.id.task_id_tv)
    TextView task_id_tv;
    @BindView(R.id.reason_tv)
    TextView reason_tv;


    Report report;
    NineGridImageViewAdapter<String> adapter1;
    NineGridImageViewAdapter<String> adapter2;


    @Override
    protected TipOffPresenter initPresenter() {
        return new TipOffPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_report_content;
    }

    @Override
    protected void initUI() {
        content1Tv.setTrimCollapsedText("展开");
        content1Tv.setTrimExpandedText("收起");
        content2Tv.setTrimCollapsedText("展开");
        content2Tv.setTrimExpandedText("收起");
        nineGridImageView1.setAdapter(adapter1 = new NineGridImageViewAdapter<String>() {
            @Override
            protected void onDisplayImage(Context context, ImageView imageView, String o) {
                Glide.with(context).load(o)
                        .apply(new RequestOptions().placeholder(com.jojo.android.zxlib.R.drawable.xui_ic_default_img))
                        .into(imageView);
            }
        });
        nineGridImageView1.setMaxSize(9);
//        nineGridImageView1.setSingleImgSize(ZxTool.dip2px(100));
        nineGridImageView1.setShowStyle(0);

        nineGridImageView2.setAdapter(adapter2 = new NineGridImageViewAdapter<String>() {
            @Override
            protected void onDisplayImage(Context context, ImageView imageView, String o) {
                Glide.with(context).load(o)
                        .apply(new RequestOptions().placeholder(com.jojo.android.zxlib.R.drawable.xui_ic_default_img))
                        .into(imageView);
            }
        });
        nineGridImageView2.setMaxSize(9);
//        nineGridImageView2.setSingleImgSize(ZxTool.dip2px(100));
        nineGridImageView2.setShowStyle(0);
    }

    @Override
    protected void initData() {

        report = (Report) getIntent().getSerializableExtra("report");
        if (report == null) {
            return;
        }
        try {
            presenter.queryTaskByOrderId(report.getBizId());
            if (!TextUtils.isEmpty(report.getReason())) {
                reason_tv.setVisibility(View.VISIBLE);
                reason_tv.setText("判定原因:" + report.getReason());
            } else {
                reason_tv.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(report.getPlaintiffContent())) {
                info1Tv.setText(report.getPlaintiffName() + "\n" +
                        "ID:" + report.getPlaintiff() + "\n" +
                        ZxTimeTool.getDate(report.getCreateTime().getTime(), "yyyy-MM-dd HH:mm"));
                Glide.with(this).load(report.getPlaintiffAvatar()).apply(new RequestOptions().placeholder(R.mipmap.test_head)).into(head1Iv);
                TipOffDialog.DataBean dataBean = new Gson().fromJson(report.getPlaintiffContent(), TipOffDialog.DataBean.class);
                content1Tv.setText("举报理由：" + dataBean.getContent());
                nineGridImageView1.setImagesData(dataBean.getUrls());
            } else {
                layout1.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(report.getDefendantContent())) {

                info2Tv.setText(report.getDefendantName() + "\n" +
                        "ID:" + report.getDefendant() + "\n" +
                        ZxTimeTool.getDate(report.getRefuteTime().getTime(), "yyyy-MM-dd HH:mm"));
                Glide.with(this).load(report.getDefendantAvatar()).apply(new RequestOptions().placeholder(R.mipmap.test_head)).into(head2Iv);
                TipOffDialog.DataBean dataBean = new Gson().fromJson(report.getDefendantContent(), TipOffDialog.DataBean.class);
                content2Tv.setText("辩诉陈词：" + dataBean.getContent());
                nineGridImageView2.setImagesData(dataBean.getUrls());
            } else {
                layout2.setVisibility(View.GONE);
            }
        } catch (Exception e) {
            ZxLogTool.err(e);
        }
    }

    @Override
    protected void regEvent() {
        nineGridImageView1.setItemImageClickListener(this);
        nineGridImageView2.setItemImageClickListener(this);
    }

    @OnClick(R.id.back)
    void back() {
        finish();
    }


    @Override
    public void onItemImageClick(ImageView imageView, int index, List list) {
        List<IPreviewInfo> infos = PreviewUtil.ImagesToPreviewInfo(list, imageView);
        PreviewBuilder.from(this)
                .setCurrentIndex(index)
                .setImgs(infos)
                .setDrag(false)
                .start();
    }

    @Override
    public void onTask(Task task) {
        if (task != null) {
            task_title_tv.setVisibility(View.VISIBLE);
            task_id_tv.setVisibility(View.VISIBLE);
            task_title_tv.setText("任务标题：" + task.getTitle());
            task_title_tv.setOnClickListener(v -> Tool.startEditTaskDetailActivity(getActivity(),task.getId()));
//            setTextViewUnderLine(task_title_tv);
            task_id_tv.setText("任务ID：" + task.getId());
        }else {
            task_title_tv.setVisibility(View.GONE);
            task_id_tv.setVisibility(View.GONE);
        }
    }

    @Override
    public void onFailure(int code, String msg) {
        task_title_tv.setVisibility(View.GONE);
        task_id_tv.setVisibility(View.GONE);
    }

    /**
     * text 设置下划线
     * @param view
     */
    public void setTextViewUnderLine(TextView view){
        //方式1
        view.setPaintFlags(view.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);
        //方式2
        view.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
    }
}
